package com.srmt.document.feishu.internal.table.model;

import com.lark.oapi.service.docx.v1.model.Block;
import com.srmt.document.feishu.internal.core.BlockContext;

/**
 * 表格操作上下文
 * 
 * 封装表格操作所需的所有上下文信息，
 * 包括文档ID、Block上下文、表格Block等。
 * 
 * @author srmt
 * @since 3.0.0
 */
public class TableContext {
    
    private final String documentId;
    private final BlockContext blockContext;
    private final Block tableBlock;
    private final TableType tableType;
    
    /**
     * 构造表格上下文
     * 
     * @param documentId 文档ID
     * @param blockContext Block操作上下文
     * @param tableBlock 表格Block
     */
    public TableContext(String documentId, BlockContext blockContext, Block tableBlock) {
        if (documentId == null || documentId.isEmpty()) {
            throw new IllegalArgumentException("文档ID不能为null或空字符串");
        }
        if (blockContext == null) {
            throw new IllegalArgumentException("块 context 不能为null");
        }
        if (tableBlock == null) {
            throw new IllegalArgumentException("表格 block 不能为null");
        }
        
        this.documentId = documentId;
        this.blockContext = blockContext;
        this.tableBlock = tableBlock;
        
        // 自动识别表格类型
        if (tableBlock.getBlockType() != null) {
            this.tableType = TableType.fromBlockType(tableBlock.getBlockType());
        } else {
            this.tableType = TableType.UNKNOWN;
        }
    }
    
    /**
     * 构造表格上下文（指定表格类型）
     * 
     * @param documentId 文档ID
     * @param blockContext Block操作上下文
     * @param tableBlock 表格Block
     * @param tableType 表格类型
     */
    public TableContext(String documentId, BlockContext blockContext, Block tableBlock, TableType tableType) {
        if (documentId == null || documentId.isEmpty()) {
            throw new IllegalArgumentException("文档ID不能为null或空字符串");
        }
        if (blockContext == null) {
            throw new IllegalArgumentException("块 context 不能为null");
        }
        if (tableBlock == null) {
            throw new IllegalArgumentException("表格 block 不能为null");
        }
        
        this.documentId = documentId;
        this.blockContext = blockContext;
        this.tableBlock = tableBlock;
        
        // 使用指定的类型，如果为null则自动识别
        if (tableType != null) {
            this.tableType = tableType;
        } else if (tableBlock.getBlockType() != null) {
            this.tableType = TableType.fromBlockType(tableBlock.getBlockType());
        } else {
            this.tableType = TableType.UNKNOWN;
        }
    }
    
    // Getters
    
    public String getDocumentId() {
        return documentId;
    }
    
    public BlockContext getBlockContext() {
        return blockContext;
    }
    
    public Block getTableBlock() {
        return tableBlock;
    }
    
    public String getTableBlockId() {
        return tableBlock.getBlockId();
    }
    
    public TableType getTableType() {
        return tableType;
    }
    
    /**
     * 判断是否为普通表格
     */
    public boolean isNormalTable() {
        return tableType == TableType.NORMAL_TABLE;
    }
    
    /**
     * 判断是否为多维表格
     */
    public boolean isBitable() {
        return tableType == TableType.BITABLE;
    }
    
    /**
     * 判断表格类型是否已知
     */
    public boolean isKnownType() {
        return tableType != TableType.UNKNOWN;
    }
    
    @Override
    public String toString() {
        return String.format("TableContext{documentId='%s', tableBlockId='%s', tableType=%s}",
                documentId, getTableBlockId(), tableType);
    }
}